<?php


namespace App\Logic\Order\Form;

use App\Core\BaseListForm;
use App\Core\Exception\DefaultException;
use App\Model\Member\MemberModel;
use App\Model\Order\OrderModel;
use App\Model\Order\OrderTaskModel;
use App\Model\UserModel;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Mysqli\QueryBuilder;

class OrderTaskListForm extends BaseListForm
{
    public $id;
    public $user_id;
    public $username;
    public $date;
    public $order_date;
    public $start_created_at;//订单创建时间 - 开始
    public $end_created_at;//订单创建时间 - 结束
    public $is_admin;//管理员

    /**
     * 订单任务列表
     * @return \App\Core\Output
     * @throws DefaultException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function lists()
    {
        $where = ['is_delete' => 0];
        if(UserIdentityHelper::getType() == UserModel::TYPE_INSIDE) {
            $this->is_admin = true;
            /**后台管理员*/
            if ($this->checkField($this->user_id)) {
                $where['user_id'] = $this->user_id;
            }
            if ($this->checkField($this->username)) {
                $subBuilder = QueryBuilder::subQuery();
                $subBuilder->where('username', $this->username);
                $subBuilder->get(MemberModel::create()->tableName(), null, "id");
                $where['user_id'] = [$subBuilder, 'in'];
            }
        }else{
            $this->user_id = UserIdentityHelper::getId();
            $where['user_id'] = $this->user_id;
            $where['created_at'] = ['2024-11-01', '>'];
        }

        if ($this->checkField($this->id)) {
            $where['id'] = $this->id;
        }

        if ($this->checkField($this->order_date) && strtotime($this->order_date) > 0) {
            $this->order_date = date('Y-m-d', strtotime($this->order_date));
            $where['created_at'] = [[$this->order_date . ' 00:00:00', $this->order_date . ' 23:59:59'], 'between'];
        }
        if ($this->checkField($this->date) && strtotime($this->date) > 0) {
            $firstDay = date('Y-m-01', strtotime($this->date)) . ' 00:00:00';
            $lastDay = date('Y-m-t', strtotime($this->date)). ' 23:59:59';
            $where['created_at'] = [[$firstDay, $lastDay ], 'between'];
        }
        /**
        创建时间检索
         */
        if ($this->checkField($this->start_created_at) && $this->checkField($this->end_created_at)) {
            /**查询时间不能跨月*/
            $start_month = date('Y-m', strtotime($this->start_created_at));
            $end_month = date('Y-m', strtotime($this->end_created_at));
            if($start_month != $end_month) {
                throw new DefaultException('查询时间不能跨月');
            }
            $where['created_at'] = [[$this->start_created_at, $this->end_created_at], 'between'];
        }elseif ($this->checkField($this->start_created_at)) {
            $where['created_at'] = [$this->start_created_at, '>='];
        }elseif ($this->checkField($this->end_created_at)) {
            $where['created_at'] = [$this->end_created_at, '<='];
        }
        $model = OrderTaskModel::makeQueryModel($where, [], $this->getPagination());

        /**@see OrderTaskModel::member() */
        $model->with(['member']);

        $model->order( 'id', 'DESC');

        $modelResult = $model->all();
        $list = [];
        foreach($modelResult as $v) {
            $item = [
                'id'                   => $v->toRawArray()['id'],
//                'user_id'              => $v->user_id,
//                'username'             => $v->member->username ?? '',
                'quantity'             => $v->quantity,
                'order_amount'         => $v->order_amount,
                'user_profit'          => $v->user_profit,
                'finished_order_count' => $v->finished_order_count,
                'total_user_profit'    => $v->total_user_profit,
                'created_at'           => $v->created_at,
                'updated_at'           => $v->updated_at == $v->created_at ? '-' : $v->updated_at,
                'created_time'         => date('m-d H:i', strtotime($v->created_at)),
            ];

            $list[] = $item;
        }

        $total = $model->lastQueryResult()->getTotalCount();
        $pagination = $this->getPagination();
        $pagination['total_count'] = $total;

        $result = [
            'list'       => $list,
            'pagination' => $pagination
        ];
        return $result;
    }

}
